﻿using System;
using System.Linq.Expressions;

namespace CoreLib
{
    public static class Guard
    {
        public static void IsNotNull<T>( Expression<Func<T>> expr ) where T : class
        {
            if( expr.Compile()() != null )
                return;

            var param = (MemberExpression)expr.Body;
            throw new ArgumentNullException( param.Member.Name );
        }

        public static void IsTrue( Func<bool> eval, string message )
        {
            if( !eval() )
                throw new ArgumentException( message );
        }

        public static void IsTrue( Func<bool> eval, string messageFormat, params object[] args )
        {
            if( !eval() )
                throw new ArgumentException( string.Format( messageFormat, args ) );
        }
    }
}
